package com.android.ailaoshan.task;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.ailaoshan.R;
import com.android.ailaoshan.task.picture.PictureFragment;
import com.android.ailaoshan.task.text.TextFragment;
import com.android.ailaoshan.task.voice.VoiceFragment;


public class TaskActivity extends AppCompatActivity {

    private Context mContext;

    private Toolbar mToolbar;
    private TextView submitTxt;
    private BottomNavigationView bottomNavigationView;

    /**
     * 碎片标识号
     * 1 文字碎片
     * 2 图片碎片
     * 3 声音碎片
     */
    private int fragmentId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);

        mContext = TaskActivity.this;

        mToolbar = findViewById(R.id.custom_toolbar);
        submitTxt = findViewById(R.id.toobar_text_submit);
        bottomNavigationView = findViewById(R.id.bottom_navigation_bar);
    }

    @Override
    protected void onStart(){
        super.onStart();

        listenButton();
    }

    @Override
    protected void onResume(){
        super.onResume();

        dynamicChangeFragment(new TextFragment());
    }

    /**
     * 点击事件监听
     */
    private void listenButton(){
        // toolbar返回按钮点击
        mToolbar.setNavigationOnClickListener(
           (v) -> Toast.makeText(mContext,"toolBar顶部图标被点击",Toast.LENGTH_SHORT).show()
        );
        // 提交按钮点击
        submitTxt.setOnClickListener(
            (v) -> Toast.makeText(mContext,"提交按钮点击监听",Toast.LENGTH_SHORT).show()
        );
        // 底部导航栏监听
        bottomNavigationView.setOnNavigationItemSelectedListener(
            (menuItem) ->  {
                int itemId = menuItem.getItemId();
                switch (itemId){
                    case R.id.bottom_item_text:{
                        if(fragmentId != 0){
                            dynamicChangeFragment(new TextFragment());
                            fragmentId = 0;
                        }
                        return true;
                    }
                    case R.id.bottom_item_pic:{
                        if(fragmentId != 1){
                            dynamicChangeFragment(new PictureFragment());
                            fragmentId = 1;
                        }
                        return true;
                    }
                    case R.id.bottom_item_voi:{
                        if(fragmentId != 2){
                            dynamicChangeFragment(new VoiceFragment());
                            fragmentId = 2;
                        }
                        return true;
                    }
                }
                return false;
            }
        );
    }

    /**
     * 动态替换碎片
     */
    private void dynamicChangeFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }
}
